<template>
  <div class="app-container">
    <hd-table-page ref="hdTable" :columns="columns" :columnsChild="columnsChild" modulePath="customer/bom" style="height: calc(100% - 120px)">
      <template #query-createTime="{ data, field }">
        <hd-date-picker keyName="createTime" v-model="data.params"></hd-date-picker>
      </template>
      <template #actionButton="{ tableData, tabDataChild }">
        <el-button type="primary" icon="el-icon-upload2" size="mini" @click="handleImport(1)">导入系列BOM</el-button>
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="exportXls(tableData)">导出</el-button>
      </template>
    </hd-table-page>
    <el-dialog :title="upload.title" v-model="upload.open" :close-on-click-modal="false" width="400px" draggable append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url"
        :disabled="upload.isUploading"
        :data="upload.data"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div class="el-upload__tip text-center" slot="tip">
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size: 12px; vertical-align: baseline" @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button @click="upload.open = false">取 消</el-button>
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
//客户机型BOM
import BaseService from '@/api/base/base'
const baseService = new BaseService('customer/bom')
import PlanService from '@/api/produce/customer'
import { getAllCustomerTwo } from '@/api/project/customer'
import { getToken } from '@/utils/auth'
//获取考勤班次
export default {
  name: 'CustomerBom',
  data() {
    return {
      // 导出遮罩层
      columns: [
        {
          prop: 'customerType',
          label: '客户',
          formatter: (row, col, val) => {
            row = col === undefined ? row.row : row
            return this.getDictLabel(row.customerType, this.allCustomerOptions, 'cusCode', 'cusName')
          },
        },
        { prop: 'customerCode', label: '客户编码' },
        { prop: 'customerProductName', label: '客户商品名称', isSearch: false },
        { prop: 'createBy', label: '创建者', isSearch: false },
        { prop: 'createTime', label: '创建时间' },
        { prop: 'updateBy', label: '更新者', isSearch: false },
        { prop: 'updateTime', label: '更新时间', isSearch: false },
        { prop: 'remark', label: '备注', isSearch: false },
      ],
      columnsChild: [
        { prop: 'parentCustomerCode', label: '父项客户编码' },
        { prop: 'productCode', label: '物料代码' },
        { prop: 'customerCode', label: '客户编码' },
        { prop: 'productNormModel', label: '品名规格' },
        { prop: 'unitUsage', label: '单位用量' },
      ],
      upload: {
        data: {},
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: '',
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: { Authorization: 'Bearer ' + getToken() },
        // 上传的地址
        url: '',
      },
      allCustomerOptions: [],
    }
  },
  created() {
    getAllCustomerTwo().then((response) => {
      this.allCustomerOptions = response.data
    })
  },
  methods: {
    handleImport(val) {
      if (val == 1) {
        this.upload.title = '导入系列BOM'
        this.upload.url = process.env.VUE_APP_BASE_API + '/customer/bom/import'
      }
      this.upload.open = true
    },
    /** 下载模板操作 */
    importTemplate() {
      PlanService.exportTemplate().then((response) => {
        this.download(response.msg)
      })
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true
    },
    submitFileForm() {
      this.$refs.upload.submit()
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false
      this.upload.isUploading = false
      if (response.data) {
        this.download(response.data.msg)
      }
      this.$alert(response.msg, '导入结果', { dangerouslyUseHTMLString: true })
      this.$refs.upload.clearFiles()
      this.getList()
    },
    exportXls(tableData) {
      this.$confirm('是否确认导出?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          return baseService.export(tableData.queryParams)
        })
        .then((response) => {
          this.download(response.msg)
        })
    },
  },
}
</script>

<style lang="scss" scoped></style>
